package com.xz.stu.server.service.impl;

import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.enums.logger.LoginLogTypeEnum;
import cn.iocoder.yudao.module.system.service.auth.AdminAuthService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import com.xz.stu.server.entity.Student;
import com.xz.stu.server.entity.Teacher;
import com.xz.stu.server.service.StudentService;
import com.xz.stu.server.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class UserLoginServiceImpl {
    @Resource
    private AdminAuthService adminAuthService;
    @Resource
    private StudentService studentService;
    @Resource
    private TeacherService teacherService;
    @Resource
    private AdminUserService userService;


    public AuthLoginRespVO login(AuthLoginReqVO reqVO) {
        //判断是否是学生否老师登录
        Student student = studentService.lambdaQuery()
                .eq(Student::getStudentId, reqVO.getUsername())
                .one();
        Teacher teacher = teacherService.lambdaQuery()
                .eq(Teacher::getTeacherId, reqVO.getUsername())
                .one();

        if (Objects.nonNull(student)){
            AdminUserDO user = userService.getUser(student.getUserId());
            reqVO.setUsername(user.getUsername());
            return adminAuthService.login(reqVO);
        }

        if (Objects.nonNull(teacher)){
            AdminUserDO user = userService.getUser(teacher.getUserId());
            reqVO.setUsername(user.getUsername());
            return adminAuthService.login(reqVO);
        }
        return adminAuthService.login(reqVO);
    }
}
